package com.wnhoo.smartfishtank.util;

import android.util.Log;

public class UtilsHex {

    private final static String TAG = "UtilsHex";

	 public static String bytesToHexString(byte[] src){  
	        StringBuilder stringBuilder = new StringBuilder("");  
	        if (src == null || src.length <= 0) {  
	            return null;  
	        }  
	        for (int i = 0; i < src.length; i++) {  
	            int v = src[i] & 0xFF;  
	            String hv = Integer.toHexString(v);  
	            if (hv.length() < 2) {  
	                stringBuilder.append(0);  
	            }  
	            stringBuilder.append(hv);  
	        }  
	        return stringBuilder.toString();  
	    }

    /**
     * bit7为1时,会出错
     * 只能把byte最大值为0x7f的值十六进制转为int[]
     * 用于日期重复
     *
     * @param b
     * @return
     */
    public static int[] byteArrayToIntArray(byte b){

        int[] result = new int[7];
        try{
            int hex_int = (int)b;
            Log.d(TAG,"Hex=0x"+Integer.toHexString(hex_int));
            String bin_str = Integer.toBinaryString(hex_int);
            int length_0 = result.length - bin_str.length();
            for (int i=0 ;i <length_0;i++){

                bin_str = "0" + bin_str;
            }
            Log.d(TAG,"bin_str=" + bin_str);

            String[] bin_str_array = bin_str.split("");
            for (int i = 0; i < bin_str_array.length; i++) {

                Log.d(TAG,"split["+i+"]="+bin_str_array[i]);
            }

            for (int i = 7; i > 0; i--) {

                Log.d(TAG,i+",bin_str_array["+i+"]="+bin_str_array[i]);
                result[i-1] = Integer.valueOf(bin_str_array[i]);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    public static byte intArrayToByte(int[] array){

        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<array.length ; i++){

            sb.append(array[i]);
        }
        int bin = Integer.valueOf(sb.toString(),2);
        return (byte)bin;
    }


    public static boolean isHexChar(String str) {
        for (int i = 0; i < str.length(); i++) {
            if ((str.charAt(i) >= '0' && str.charAt(i) <= '9')
                    || (str.charAt(i) >= 'a' && str.charAt(i) <= 'f')
                    || (str.charAt(i) >= 'A' && str.charAt(i) <= 'F')) {
            } else {
                return false;
            }
        }
        return true;
    }
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }
        return d;
    }

    /**
     * 这是RGB值的转换
     * @param color
     * @return
     */
    public static String bytesToHexStringColor(byte [] color){

        String r,g,b,alpha;
        r = Integer.toHexString(color[0]).toUpperCase();
        g = Integer.toHexString(color[1]).toUpperCase();
        b = Integer.toHexString(color[2]).toUpperCase();
        alpha = Integer.toHexString(color[3]).toUpperCase();

        r = r.length()==1 ? "0" + r : r ;
        g = g.length()==1 ? "0" + g : g ;
        b = b.length()==1 ? "0" + b : b ;
        alpha = b.length()==1 ? "0" + alpha : alpha ;

        StringBuffer sb = new StringBuffer();
        sb.append("#").append(alpha).append(r).append(g).append(b);
        return sb.toString();
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}
